package ui.videos 
{
	import com.gaiaframework.api.Gaia;
	import com.gaiaframework.api.IPageAsset;
	import com.greensock.easing.Quint;
	import com.greensock.TweenLite;
	import com.somerandomdude.coordy.layouts.twodee.HorizontalLine;
	import com.somerandomdude.coordy.layouts.twodee.ILayout2d;
	import com.video.VideoPlayerEvent;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import ui.videos.Control;
	import ui.videos.FriseItem;
	import utils.ShapeUtil;
	import utils.SpriteUtil;
	
	/**
	 * ...
	 * @author usual
	 */
	public class Frise extends MovieClip 
	{
		private var _chaptersArray:Array;
		private var _currentVideoPlayer:*;//SlideItem
		private var _container:MovieClip;
		private var _layout:ILayout2d;
		private var _cb:String;
		private var _cp:IPageAsset;
		private var _ci:String;
		private var _currentFriseItem:FriseItem;
		private var _control:Control;
		private var _friseLine:Sprite
		private var _maskFriseLine:Sprite;
		//private var _maskFriseLineInteract:Sprite;
		public function Frise(chaptersArray:Array,obj:*) 
		{

			_currentVideoPlayer = obj;
			_chaptersArray = chaptersArray;
			addEventListener(Event.ADDED_TO_STAGE, build);
			addEventListener(Event.REMOVED_FROM_STAGE, removeListeners);
			
		}
		
		private function removeListeners(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, removeListeners);
			_container.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveEvent);
		}
		
		public function update(obj:*):void 
		{
			_cb = Gaia.api.getCurrentBranch();
			_cp = Gaia.api.getPage(_cb);
			_ci = _cp.id;
			_currentVideoPlayer = obj;
			_control.videoPlayer = _currentVideoPlayer.playerVideo;
			_control.update();
			activate(_ci);
			_currentVideoPlayer.addEventListener(VideoPlayerEvent.VIDEO_PLAY_HEAD_UPDATE , onVideoPlaying);
			
		}
		
		public function resize():void 
		{
			var sw:Number = stage.stageWidth -322;
			_control.resize();
			_container.y = _control.y + _control.height + 14;
			//_container.x = int(_control.x + 70);
			if (_friseLine)
			{
				_friseLine.width = sw;
				_maskFriseLine.width = stage.stageWidth;
				//_maskFriseLineInteract.width = sw;
				_friseLine.x =  _control.seekBar.x + 15;
				_maskFriseLine.x = (stage.stageWidth / 2) - (_maskFriseLine.width / 2);
				//_maskFriseLineInteract.x = 
				_friseLine.y = 25;
				_maskFriseLine.y = -456;
				//_maskFriseLineInteract.y 
			}
		}
		
		private function onVideoPlaying(e:Event):void 
		{
			//trace("[Frise.onVideoPlaying] ",_currentVideoPlayer.playHeadPercentage);
			if (_control)
			{
				_control.seekBar.update(_currentVideoPlayer.playHeadPercentage);
				_control.timeText.update(_currentVideoPlayer.playHeadTime);
				
			}
		}
		
		private function activate(ci:String):void 
		{
			var fi:FriseItem = _container.getChildByName(ci) as FriseItem;
			if (_currentFriseItem)
			{
				if (_currentFriseItem != fi)
				{
					deactivate(_currentFriseItem);
				}
			}
			_currentFriseItem = fi;
			_currentFriseItem.activate();
		}
		
		private function deactivate(fi:FriseItem):void 
		{
			fi.deactivate();
		}
		
		private function build(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, build);
			addLine();
			addControls();
			var ln:Number = _chaptersArray.length;
			_container = new MovieClip();
			addChild(_container);
			_layout = new HorizontalLine(100);
			var fk:Sprite = SpriteUtil.drawRectSprite(300, 20);
			fk.alpha = 0;
			_container.addChild(fk);
			_layout.addNode(fk);
			for (var i:int = 0; i < ln; i++) 
			{
				var _friseItem:FriseItem = new FriseItem(Object(_chaptersArray[i]));
				_friseItem.name = _chaptersArray[i].pageId;
				_friseItem.buttonMode = true;
				_container.addChild(_friseItem);
				_layout.addNode(_friseItem);
				_layout.updateAndRender();
			}
			trace("[Frise.build] ", _container.width);
			var surface:Sprite = SpriteUtil.drawRectSprite(_container.width, 100);
			surface.name = "surface";
			surface.alpha = 0;
			_container.addChildAt(surface,0);
			var fk2:Sprite = SpriteUtil.drawRectSprite(200, 20);
			fk2.alpha = 0;
			_container.addChild(fk2);
			_layout.addNode(fk2);
			_maskFriseLine.height = _container.height;
			//_maskFriseLineInteract.height=
			_container.mask = _maskFriseLine;
			//_maskFriseLineInteract.alpha = 0;
			_container.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveEvent);
		}
		
		private function mouseMoveEvent(e:MouseEvent):void 
		{
			var xR:Number = _container.width - stage.stageWidth;
			var sP:Number = (e.stageX-_maskFriseLine.x) / _maskFriseLine.width;
			var rX:Number = xR * sP;
			TweenLite.to(_container, 3, {x: -rX, ease:Quint.easeOut})		
		}
		
		private function addLine():void 
		{
			_friseLine = ShapeUtil.getSprite(100, 1, 0xffffff);
			_friseLine.name = "_friseLine";
			_maskFriseLine = ShapeUtil.getSprite(100, 100, 0xffffff);
			_maskFriseLine.name = "_maskFriseLine";
			//_maskFriseLineInteract = ShapeUtil.getSprite(100, 100, 0xffffff);
			//_maskFriseLineInteract.name = "_maskFriseLineInteract";
			addChild(_friseLine);
			addChild(_maskFriseLine);
			//addChild(_maskFriseLineInteract);
		}
		
		private function addControls():void 
		{
			_control = new Control();
			_control.videoPlayer = _currentVideoPlayer.playerVideo;
			_control.addEventListener(VideoPlayerEvent.VIDEO_PAUSE,pauseCurrentVideoPlayer);
			_control.addEventListener(VideoPlayerEvent.VIDEO_PLAY,playCurrentVideoPlayer);
			addChild(_control);
		}
		
		private function playCurrentVideoPlayer(e:VideoPlayerEvent):void 
		{
			_currentVideoPlayer.unPauseVideo();
		}
		
		private function pauseCurrentVideoPlayer(e:VideoPlayerEvent):void 
		{
			_currentVideoPlayer.pauseVideo();
		}
		
	}

}